﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using KeaoBase;
using System.Reflection;
using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace KeaoDataService
{
    public enum UploadType
    {
        水稳料=0,
        混凝土=1,
        沥青料=2
    }

  public class BaseUpLoadInfo
    {
      protected int m_LastNum { get; set; }
      protected int m_Refresh = 300000;//刷新时间
      protected Thread m_UpLoadThread;
      private BaseUpLoad m_UpLoad;
      protected UploadType m_CurrentUploadType;

      private bool m_UpLoadStatus = false;
      public bool UpLoadStatus
      {
          get
          {
              return m_UpLoadStatus;
          }
      }

      protected void SendSMS(string Str, string PhoneNum)
      {
          m_UpLoad.SendSMS(Str, PhoneNum);
      }

      public virtual bool Init()
      {
          try
          {
              m_UpLoad = Assembly.LoadFrom("KeaoUpLoad.dll").CreateInstance(string.Format("KeaoUpLoad.{0}", KeaoUtils.ConfigGetNodeAttr("网络设备", "类型"))) as BaseUpLoad;
              m_UpLoad.OnReceiveData += new OnReceiveDataHandler(m_UpLoad_OnReceiveData);
              if (m_UpLoad.Init(KeaoUtils.ConfigGetNodeAttr("网络设备", "地址")))
              {
                  m_UpLoadThread = new Thread(new ThreadStart(UpLoadData));
                  m_UpLoadThread.Start();
                  m_UpLoadStatus = true;
                  return m_UpLoadStatus;
              }
              else
              {
                  return false;
              }
          }
          catch (Exception err)
          {
              KeaoUtils.LogTxt(string.Format("初始化上传异常。{0}", err.Message));
              return false;
          }
      }

      void m_UpLoad_OnReceiveData(object sender, DataInfoEventArgs e)
      {
          try
          {
              JArray jArray = JsonConvert.DeserializeObject(e.m_DataInfo.CurrentJson) as JArray;
              switch (e.m_DataInfo.CurrentByte)
              {
                  case 0x33:
                      TableRow shuiWenRow = new TableRow("tbl_banhezhan", string.Format("{0}", jArray[0]["id"]), DBCtl.修改);
                      shuiWenRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                      KeaoUtils._DB.SaveTableRows(shuiWenRow);
                      break;
                  case 0x36:
                      TableRow huNingTuRow = new TableRow("tbl_hunningtu", string.Format("{0}", jArray[0]["id"]), DBCtl.修改);
                      huNingTuRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                      KeaoUtils._DB.SaveTableRows(huNingTuRow);
                      break;
                  case 0x37:
                      TableRow liQingRow = new TableRow("tbl_liqing", string.Format("{0}", jArray[0]["id"]), DBCtl.修改);
                      liQingRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                      KeaoUtils._DB.SaveTableRows(liQingRow);
                      break;
              }
          }
          catch (Exception err)
          {
              KeaoUtils.LogTxt(string.Format("解析回发数据异常。{0}", err.Message));
          }
      }


      private void UpLoadData()
      {
          string createCondition = KeaoUtils._DB.CreateAndCondition("shangchuanzhuangtai", DataValueCommonType.字符串,"0","", SearchConditionType.等于);
          while (true)
          {
              try
              {
                  switch (m_CurrentUploadType)
                  {
                      case UploadType.水稳料:
                          DataTable shuiWenDt = KeaoUtils._DB.GetDataTableByCondition("tbl_banhezhan", "*", null, createCondition, null, null, 1);
                          m_UpLoad.SendData(0x13, shuiWenDt);
                          break;
                      case UploadType.混凝土:
                          DataTable hunNingTuDt = KeaoUtils._DB.GetDataTableByCondition("tbl_hunningtu", "*", null, createCondition, null, null, 1);
                          m_UpLoad.SendData(0x16, hunNingTuDt);
                          break;
                      case UploadType.沥青料:
                          DataTable liQingDt = KeaoUtils._DB.GetDataTableByCondition("tbl_liqing", "*", null, createCondition, null, null, 1);
                          m_UpLoad.SendData(0x17, liQingDt);
                          break;
                  }
                  Thread.Sleep(10000);
              }
              catch (ThreadAbortException)
              {
                  return;
              }
              catch (Exception err)
              {
                  KeaoUtils.LogTxt(string.Format("上传数据异常。{0}", err.Message));
              }
          }
      }

      public virtual void Close()
      {
          if (m_UpLoadThread != null)
          {
              m_UpLoadThread.Abort();
          }
          if (m_UpLoad != null)
          {
              m_UpLoad.CloseDevice();
          }
      }

    }
}
